<?php

class Order
{
    var $pdo = null;
    var $logger = null;

    function __construct()
    {
        $this->pdo = PDOWrapper::instance();
        $this->logger = CLog::getInstance();
    }

    function create_order($userinfo){

        list($user_id, $r_id, $table_id,$login_time) = $userinfo;
        $order = $user_id.User::TOKEN_DELIM.$r_id.User::TOKEN_DELIM.$table_id.User::TOKEN_DELIM.time();
        $crypt = new MyCrypt(CRYPT_KEY);
        $order = $crypt->encrypt($order);

        return $order;
    }


    // 查询已下单
    function CheckOrderV1($GetParams, $PostArray){

        $user_info = User::GetInfoByToken($GetParams['token']);
        if($user_info === false) {
            return false;
        }

        $field = ['order_id','time'];
        $params = [
            'r_id' => $user_info['r_id'],
            'table_id' => $user_info['table_id'],
            'status' => 0 
        ];
        $order_info = $this->pdo->select('orders',$field,$params);
        if ($order_info === false) {
            Utils::ShowError(ErrorDefine::DATABASE_ERROR);
            return false;
        }

        $order = $this->pdo->query("SELECT order_detail.order_num,order_detail.time,order_detail.`status`,recipe.`name`,recipe.pic_url,recipe.type_id,recipe_type.type_name,recipe.price,users.user_name,users.user_pic,users.user_id FROM order_detail LEFT JOIN recipe ON recipe.recipe_id=order_detail.recipe_id LEFT JOIN users ON users.user_id=order_detail.user_id LEFT JOIN recipe_type ON recipe_type.type_id=recipe.type_id WHERE order_detail.order_id=".$order_info[0]['order_id']."");
        
        $table = new Table();
        $desc = $table->sort_type($order);
        
        $description = '';
        $num = 0;
        foreach ($desc as $key => $value) {
            $num += count($value['list']); 
            $description .= $value['type_name'].count($value['list']).'个，';
        }
        $description = mb_substr('合计'.$num.'个：'.$description, 0,-1);
        
        //计算总金额
        $total = 0;
        foreach ($order as $key => $value) {
            $total += $value['price']*$value['order_num']; 
        }
        $order_list = self::sort_by_user($order);

        foreach ($order_list as $key => $value) {
            $per_num = 0;
            foreach ($value['list'] as $k => $v) {
                $per_num += $v['order_num'];
            }
            $order_list[$key]['per_num'] = $per_num;
        }


        $table_info = $table->table_info($user_info);
        
        $date_time = strtotime($order_info[0]['time']);
        $message = [
            'total' => number_format($total/100,2,".",""),
            'time' => date("H:i",$date_time),
            'table_id' => $user_info['table_id'],
            'description' => $description,
            'users_num' => $table_info['users_num'],
            'order_list' => $order_list
        ];

        Utils::ShowMessage($message);
        return true;
    }

    private function sort_by_user($order,$order_list = array()){

        if (empty($order)) {
            return $order_list;
        }
        $order[0]['price'] = number_format($order[0]['price']/100,2,".","");
        $date_time = strtotime($order[0]['time']);
        $order[0]['time'] = date("H:i",$date_time);
        if (empty($order_list)) {
            $order_list[0]['user_id'] = $order[0]['user_id'];
            $order_list[0]['user_name'] = $order[0]['user_name'];
            $order_list[0]['user_pic'] = $order[0]['user_pic'];
            $order_list[0]['time'] = $order[0]['time'];
            $order_list[0]['list'] = [];
            array_push($order_list[0]['list'], $order[0]);
            array_splice($order, 0, 1);
            return self::sort_by_user($order,$order_list);
        }
        $flag = false;
        foreach ($order_list as $key => $value) {
            if ($value['user_id'] == $order[0]['user_id']) {
                $flag = true;
                array_push($order_list[$key]['list'], $order[0]);
                array_splice($order, 0, 1);
            }
        }
        $temp = [];
        if ($flag === false) {
            $temp['user_id'] = $order[0]['user_id'];
            $temp['user_name'] = $order[0]['user_name'];
            $temp['user_pic'] = $order[0]['user_pic'];
            $temp['time'] = $order[0]['time'];
            $temp['list'] = [];
            array_push($temp['list'], $order[0]);
            array_push($order_list, $temp);
            array_splice($order, 0, 1);
        }
        return self::sort_by_user($order,$order_list);
    }
}